import WS_ADDRESS from '../configs';

function useWebsocket(handleMessage) {
  const ws = new WebSocket(WS_ADDRESS);
  function handleOpen(e) {
    console.log('Websocket open', e);
  }
  function handleClose(e) {
    console.log('Websocket close', e);
  }
  function handleError(e) {
    console.log('Websocket error', e);
  }
  function bindEvent() {
    ws.addEventListener('open', handleOpen, false);
    ws.addEventListener('close', handleClose, false);
    ws.addEventListener('error', handleError, false);
    ws.addEventListener('message', handleMessage, false);
  }
  const init = () => {
    bindEvent();
  };
  init();
  return ws;
}
export default useWebsocket;
